/**
* @file			  This is the flie for relay status and change
* @author  		Salina Joseph - student ID = st20000439
* @version 		0.2
* @date    		December 2014
* @brief   		Arduino compatibility module 
*/

/* Includes ------------------------------------------------------------------*/
#include "arduino.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define digitalPinToPort(P) 	( DIGITAL_PIN_TO_PORT[P] )
#define digitalPinToBitMask(P) 	( DIGITAL_PIN_TO_BIT_MASK[P] )

/* Private variables ---------------------------------------------------------*/
GPIO_TypeDef *DIGITAL_PIN_TO_PORT[14] = { GPIOA, GPIOA, GPIOB, GPIOB, GPIOA, GPIOB, GPIOB, GPIOA, GPIOA, GPIOA, GPIOA, GPIOB, GPIOB, GPIOB };
uint16_t DIGITAL_PIN_TO_BIT_MASK[14] = {GPIO_Pin_3, GPIO_Pin_2, GPIO_Pin_12, GPIO_Pin_11, GPIO_Pin_7, GPIO_Pin_9, GPIO_Pin_8, GPIO_Pin_6, GPIO_Pin_5, GPIO_Pin_4, GPIO_Pin_11, GPIO_Pin_5, GPIO_Pin_4, GPIO_Pin_3};
static uint32_t msTick = 0;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

// SysTick handler to implement timebase features.
void SysTick_Handler()
{
	msTick++;
}


/**
* @brief	Initialization code to configure STM32F0 Discovery board for Arduino compatibility
* @param	None
* @retval	None
*/
void platform_config()
{
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
	SysTick_Config(SystemCoreClock/1000);
}


/**
* @brief	this is configure the pin to be either input or output. 
* @param	pin		the pin you set manually
* @param	mode	this will allow INPUT or OUTPUT
* @retval	None
*/
void pinMode(uint8_t pin, int mode)
{
	GPIO_InitTypeDef gpio_init_struct;
	GPIO_TypeDef *gpio_port = digitalPinToPort(pin);
	uint16_t gpio_pin = digitalPinToBitMask(pin);
	
	if (mode == OUTPUT) {
		gpio_init_struct.GPIO_Mode = GPIO_Mode_OUT;
		gpio_init_struct.GPIO_OType = GPIO_OType_PP;
		gpio_init_struct.GPIO_Speed = GPIO_Speed_Level_3;
		gpio_init_struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
		gpio_init_struct.GPIO_Pin = gpio_pin;
	} else {
		gpio_init_struct.GPIO_Mode = GPIO_Mode_IN;
		gpio_init_struct.GPIO_PuPd = GPIO_PuPd_UP;
		gpio_init_struct.GPIO_Pin = gpio_pin;		
	}
	GPIO_Init(gpio_port, &gpio_init_struct);
}


/**
* @brief	reads a value from a digital pin whether it is HIGH or LOW
* @param	pin		the number of the digital pin you want to read
* @retval	HIGH or LOW
*/
int digitalRead(uint8_t pin)
{
	GPIO_TypeDef *gpio_port = digitalPinToPort(pin);
	uint16_t gpio_pin = digitalPinToBitMask(pin);
	int rd_val = GPIO_ReadInputDataBit(gpio_port,gpio_pin);
	if(rd_val ){
		return HIGH;
	}
	return LOW;
}


/**
* @brief	Write a HIGH or a LOW value to a digital pin.
* @param	pin		the pin number
* @param	value	HIGH or LOW
* @retval	None
*/
void digitalWrite(uint8_t pin, int value)
{
	GPIO_TypeDef *gpio_port = digitalPinToPort(pin);
	uint16_t gpio_pin = digitalPinToBitMask(pin);
	
	if (value == 1) {
		GPIO_WriteBit(gpio_port, gpio_pin, Bit_SET); 
	} else {
		GPIO_WriteBit(gpio_port, gpio_pin, Bit_RESET);
	}
}


/**
* @brief	Pauses the program for the amount of time (in miliseconds) specified as parameter. 
* @param	ms		the number of milliseconds to pause
* @retval	None
*/
void delay(uint32_t ms)
{
	uint32_t start = msTick;
	while ((msTick - start) < ms);
}


/**
* @brief	Returns the number of milliseconds since the board began running the current program. 
* @param	None
* @retval	Number of milliseconds since the program started
* @remark	This number will overflow (go back to zero), after approximately 50 days.
*/
uint32_t millis(void)
{
	return msTick;
}
